import { createRouter, createWebHashHistory, createWebHistory } from "vue-router";
import { useUser } from "@/store/modules/user";

const routes = [
  {
    path: "/",
    component: () => import("@/layout/index.vue"),
    redirect: "/message",
    children: [
      {
        path: "message",
        component: () => import("@/pages/Message/index.vue"),
      },
      {
        path: "friend",
        component: () => import("@/pages/Friend/index.vue"),
      },
    ],
  },
  { path: "/login", component: () => import("@/pages/Login/index.vue") },
  { path: "/register", component: () => import("@/pages/Register/index.vue") },
  { path: "/moments", component: () => import("@/pages/Moments/index.vue") },
  {
    path: "/file",
    component: () => import("@/pages/File/index.vue"),
  },
  {
    path: "/addFriend",
    component: () => import("@/pages/AddFriend/index.vue"),
  },
  {
    path: "/preview",
    component: () => import("@/pages/PreviewImage/index.vue"),
  },
  {
    path: "/screenshot",
    component: () => import("@/pages/ScreenShot/index.vue"),
  },
  {
    path: "/systemSetting",
    component: () => import("@/pages/SystemSetting/index.vue"),
    redirect: "/SystemSetting/common",
    children: [
      {
        path: "common",
        meta: {
          name: "通用",
        },
        component: () => import("@/pages/SystemSetting/views/Common.vue"),
      },
      {
        path: "lock",
        meta: {
          name: "锁定设置",
        },
        component: () => import("@/pages/SystemSetting/views/Lock.vue"),
      },
      {
        path: "fileStore",
        meta: {
          name: "存储设置",
        },
        component: () => import("@/pages/SystemSetting/views/FileStroe.vue"),
      },
      {
        path: "privacy",
        meta: {
          name: "隐私管理",
        },
        component: () => import("@/pages/SystemSetting/views/Privacy.vue"),
      },
      {
        path: "theme",
        meta: {
          name: "多彩主题",
        },
        component: () => import("@/pages/SystemSetting/views/Theme.vue"),
      },
    ],
  },
  {
    path: "/videoCall",
    component: () => import("@/pages/VideoCall/index.vue"),
  },
  {
    path: "/voiceCall",
    component: () => import("@/pages/VoiceCall/index.vue"),
  },
  {
    path: "/notify",
    component: () => import("@/pages/Notify/index.vue"),
  },
  {
    path: "/aiChat",
    component: () => import("@/pages/AiChat/index.vue"),
  },
  {
    path: "/quickAsk",
    component: () => import("@/pages/QuickAsk/index.vue"),
  },
  {
    path: "/systemLock",
    component: () => import("@/pages/SystemLock/index.vue"),
  },
  {
    path: "/weatherForecast",
    component: () => import("@/pages/WeatherForecast/index.vue"),
  },
];

const router = createRouter({
  history: import.meta.env.DEV ? createWebHistory() : createWebHashHistory(),
  routes,
});

const whiteList = ["/login", "/register"];

router.beforeEach((to, _from, next) => {
  const user = useUser();
  user.token || whiteList.includes(to.path) ? next() : next("/login");
});

export default router;
